﻿#pragma once
#ifndef SpinLock_hpp
#define SpinLock_hpp

#include<atomic>
#include<thread>
#include"./MutexInterface.hpp"

namespace MMF{

class SpinLock: public MutexInterface
{

private:

    std::atomic<bool> mLocked = false;
    
public:
    
    ~SpinLock() {mLocked = false;}
    
    
    bool Locked()
    {
        return mLocked;
    }
    
    void Lock()
    {
        while(mLocked.load());
        mLocked = true;
    }
    
    void Unlock()
    {
        mLocked = false;
    }

    bool TryLock()
    {
        bool old = false;
        return mLocked.compare_exchange_strong(old, true, std::memory_order_acquire);
    }
};


}


#endif /* SpinLock_h */
